package generic.net.observers;

import generic.net.Message;
import java.util.Vector;

/**
 * Notifys a group of observers that someone is shouting at them.
 * @author Adam Scarr <scarr.adam@gmail.com>
 */
public class BroadcastDispatcher {
    private Vector<BroadcastObserver> observers = new Vector<BroadcastObserver>();

    /**
     * Register yourself to be shouted at.
     * @param observer  You.
     */
    public synchronized void add(BroadcastObserver observer) {
        observers.add(observer);
    }

    /**
     * Stop getting shouted at.
     * @param observer  You.
     */
    public synchronized void remove(BroadcastObserver observer) {
        observers.remove(observer);
    }

    /**
     * When you feel like shouting, call this method.
     * @param message   Whatcha shouting about?
     */
    public synchronized void notifyBroadcast(Message message) {
        for (BroadcastObserver observer: observers) {
            observer.send(message);
        }
    }

    /**
     * @return  The number of observers
     */
    public synchronized int size() {
        return observers.size();
    }
}
